//
//  FSDecorateMotailViewController.m
//  Freemode
//
//  Created by xhl on 16/3/14.
//  Copyright © 2016年 贵州喜乐荟科技有限公司. All rights reserved.
//

#import "FSDecorateMotailViewController.h"
#import "FSMotailCollectionViewCell.h"
//自定义导航栏  的一个封装  多看看
#import "HCNavigationBar.h"


@interface FSDecorateMotailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    CGFloat _offset;
}
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UILabel *pageLbl;


@end

static NSString *cellIdentifier = @"cellIdentifier";
@implementation FSDecorateMotailViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
//    [HCNavigationBar setNavigationTitle:self withTitle:@"删除照片"];

    self.title = @"删除照片";
    if (_isShowDeleBt) {
        [HCNavigationBar setNavigationBarRightButton:self withBtnNormalImg:nil withBtnPresImg:nil withTitle:@"删除" withAction:@selector(deletPhoto)];
    }
    
    
    
    _offset = 0.0;
    [self initWithCollectionView];
    
    [self initWithTitleView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.callBackBlock) {
        self.callBackBlock(_dataArr);
    }
}

//返回
- (void)back
{
    if (self.callBackBlock) {
        self.callBackBlock(_dataArr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
//删除
- (void)deletPhoto
{
//    NSMutableArray *arr  = [NSMutableArray arrayWithArray:_dataArr];
    [_dataArr removeObjectAtIndex:_seletTag];
    [_collectionView reloadData];
    if (_dataArr.count == 0) {
        [self back];
        return;
    }
    NSInteger item = _seletTag>0?_seletTag:1;
    _pageLbl.text = $str(@"%ld/%ld",item+1,_dataArr.count);
}

- (void)initWithCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH_BOUNDS, SCREEN_HEIGHT_BOUNDS-64);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_BOUNDS, SCREEN_HEIGHT_BOUNDS-64) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //翻页模式，不会一直滚动
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[FSMotailCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    [self.view addSubview:_collectionView];
    //当前选中页面
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH_BOUNDS*_seletTag, SCREEN_HEIGHT_BOUNDS-64);
}

- (void)initWithTitleView
{
    _pageLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    _pageLbl.text = $str(@"%ld/%ld",_seletTag+1,_dataArr.count);
    _pageLbl.textAlignment = NSTextAlignmentCenter;
    _pageLbl.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = _pageLbl;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FSMotailCollectionViewCell *cell = (FSMotailCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell cellDataWithImg:_dataArr[indexPath.row]];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = SCREEN_WIDTH_BOUNDS;
    int page = floor((self.collectionView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    _pageLbl.text = $str(@"%d/%ld",page+1,_dataArr.count);
    _seletTag = page;
}

@end
